#!/usr/bin/perl
use CGI;
use CGI qw/:standard/;
use CGI::Pretty qw/:standard *-table *-div/;
use FindBin '$Bin';
use lib $Bin;
use DBI;
use Data::Dumper;
use CgiUtil;

my $g_task_id;
my $g_ex = 0;
my %g_table;

sub get_detail_data {
	my $content = "";
	my $db_name = "d_stock";
	my $db_host = "localhost";
	my $db_user = "root";
	my $db_pwd = "123456";
	my $dbh = DBI->connect("dbi:mysql:$db_name:$db_host", $db_user, $db_pwd);

        my $sql = "select de.c_date,de.c_operate,de.c_code,de.c_name,de.c_price,de.c_rate,de.c_volume,de.c_earn,h.c_hold,h.c_all,de.c_reason from (select d.c_task_id,d.c_date,d.c_operate,d.c_code,n.c_name,d.c_price,d.c_rate,d.c_volume,d.c_earn,d.c_reason from t_task_detail as d right join t_stock_names_A as n on d.c_code=n.c_code  where d.c_task_id=$g_task_id order by d.c_date asc,d.c_operate desc) as de inner join t_task_hold as h on de.c_date=h.c_date and de.c_task_id=h.c_task_id";
        my $rows = $dbh->selectall_arrayref($sql);
	my $cnt = 0;
	foreach (@$rows) {
		my ($date,$operate,$code,$name,$price,$rate,$volume,$earn,$hold,$all,$reason) = @$_;
		$cnt ++;
		$g_table{"$cnt"}{"date"} = $date;
		$g_table{"$cnt"}{"operate"} = $operate eq "buy" ? "买入" : "卖出";
		$g_table{"$cnt"}{"code"} = $code;
		$g_table{"$cnt"}{"name"} = $name;
		$g_table{"$cnt"}{"price"} = $price;
		$g_table{"$cnt"}{"rate"} = $rate;
		$g_table{"$cnt"}{"volume"} = $volume;
		$g_table{"$cnt"}{"earn"} = sprintf("%.2f",$earn);
		$g_table{"$cnt"}{"hold"} = sprintf("%.2f",$hold);
		$g_table{"$cnt"}{"all"} = sprintf("%.2f",$all);
		$g_table{"$cnt"}{"reason"} = $reason;
	}
#	print Dumper(\%g_table);
	if ($g_ex == 1) {
		$content .= '<table border="1"><tr><th>日期</th><th>操作</th><th>代码</th><th>名称</th><th>价格</th><th>收益率</th><th>股数</th><th>收益</th><th>当前持仓</th><th>总价值</th><th>K线图</th><th>明细</th></tr>';
		foreach my $key (sort {$a<=>$b} keys %g_table) {
			$content .= "<tr><td>$g_table{$key}{'date'}</td>" 
				. "<td>$g_table{$key}{'operate'}</td>" 
				. "<td><a href=\"http://stock.quote.stockstar.com/realtime_$g_table{$key}{'code'}.shtml\" target=\"_blank\">$g_table{$key}{'code'}</a></td>" 
				. "<td>$g_table{$key}{'name'}</td>"
				. "<td>$g_table{$key}{'price'}</td>" 
				. "<td>$g_table{$key}{'rate'}</td>" 
				. "<td>$g_table{$key}{'volume'}</td>" 
				. "<td>$g_table{$key}{'earn'}</td>" 
				. "<td>$g_table{$key}{'hold'}</td>" 
				. "<td>$g_table{$key}{'all'}</td>" 
				. "<td><a href=\"http://stock.quote.stockstar.com/realtime_$g_table{$key}{'code'}.shtml\" target=\"_blank\">K线图</a></td>" 
				. "<td>$g_table{$key}{'reason'}</td></tr>";
		}
	} else {
		$content .= '<table border="1"><tr><th>日期</th><th>操作</th><th>代码</th><th>名称</th><th>价格</th><th>收益率</th><th>股数</th><th>收益</th><th>当前持仓</th><th>总价值</th><th>K线图</th></tr>';
		foreach my $key (sort {$a<=>$b} keys %g_table) {
			$content .= "<tr><td>$g_table{$key}{'date'}</td>" 
				. "<td>$g_table{$key}{'operate'}</td>" 
				. "<td><a href=\"http://stock.quote.stockstar.com/realtime_$g_table{$key}{'code'}.shtml\" target=\"_blank\">$g_table{$key}{'code'}</a></td>" 
				. "<td>$g_table{$key}{'name'}</td>"
				. "<td>$g_table{$key}{'price'}</td>"
				. "<td>$g_table{$key}{'rate'}</td>" 
				. "<td>$g_table{$key}{'volume'}</td>" 
				. "<td>$g_table{$key}{'earn'}</td>" 
				. "<td>$g_table{$key}{'hold'}</td>" 
				. "<td>$g_table{$key}{'all'}</td>" 
				. "<td><a href=\"http://stock.quote.stockstar.com/realtime_$g_table{$key}{'code'}.shtml\" target=\"_blank\">K线图</a></td>" 
				. "</tr>";
		}
	}
	return $content;
}

sub get_condition_data {
	my $content = "";
	my $db_name = "d_stock";
	my $db_host = "localhost";
	my $db_user = "root";
	my $db_pwd = "123456";
	my $dbh = DBI->connect("dbi:mysql:$db_name:$db_host", $db_user, $db_pwd);

        my $sql = "select c_condition from t_daily_task where c_task_id=$g_task_id";
        my $rows = $dbh->selectall_arrayref($sql);
	my $cond;
	foreach (@$rows) {
		($cond) = @$_;
	}
	my @conds = split /\|/, $cond;
	my $content = '<table border="0">';

	my $money = $conds[1];
	my $buy_cond = $conds[4];
	my $hold_day_lower = $conds[5];
	my $hold_day_uper = $conds[6];
	my $sell_cond = $conds[9];
	my $money_weight = $conds[10];
	my $start = $conds[11];
	my $end = $conds[12];

	$content .= "<tr><td>初始资产:                             </td><td>$money</td></tr>";

	$content .= "<tr><td>买入条件:</td><td>";
	my $buy_cnt = 0;
	while ($buy_cond =~ /(.+?);/g) {
		$buy_cnt ++;
		my $buy_one = $1;
		if ($buy_one =~ /(\d+)-(\d+):(.+?),(.+?):(.+?),(.+?)-(.+?):(.+?)$/) {
			my $forward;
			if ($5>0) {$forward= "涨优先";} else {$forward="跌优先";}
			$content .= "买入条件$buy_cnt:$1天~$2天，涨幅泛围[$3,$4],$forward,价格泛围[$6,$7],条件比重：$8<br>";
		}
	}
	$content .= "</td></tr>";

	$content .= "<tr><td>至少持仓天数:</td><td>$hold_day_lower</td></tr>";
	$content .= "<tr><td>至多持仓天数:</td><td>$hold_day_uper</td></tr>";

	$content .= "<tr><td>卖出条件:</td><td>";
	my $sell_cnt = 0;
	while ($sell_cond =~ /(.+?);/g) {
		$sell_cnt ++;
		my $sell_one = $1;
		if (!($sell_one =~ /x/)) {
			if ($sell_one =~ /(\d+)-(\d+):(.+?),(.+?)$/g)
			{
				$content .= "固定条件$sell_cnt:第$2天相对于第$1天，涨幅低于$3,或高于$4则卖出<br>";
			}
		}
		if ($sell_one =~ /x/) {
			if ($sell_one =~ /x(\d+)-(\d+):(.+?),(.+?)$/g)
			{
				$content .= "循环条件$sell_cnt:从$1天起每$2天，涨幅低于$3,或高于$4则卖出<br>";
			}
		}
	}
	$content .= "</td></tr>";

	$content .= "<tr><td>初始资产分配比例:</td><td>$money_weight</td></tr>";
	$content .= "<tr><td>开始购买时间:</td><td>$start</td></tr>";
	$content .= "<tr><td>结束购买时间:</td><td>$end</td></tr>";

	$content .= "</table>";
	return $content;
}

sub main {
	print myhead("操作明细");

	$g_task_id = url_param('task_id');
	$g_ex = url_param('ex');
	if (!defined($g_task_id)) {
		print h3("仿真编号不正确");
	} else {
		print h3("仿真编号:$g_task_id");
		print h3("仿真条件");
		print get_condition_data($g_task_id);
		print h3("操作明细");
		print get_detail_data($g_task_id);
	}
}

main();
